package com.guet.main.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guet.common.utils.R;
import com.guet.main.entity.Credentials;
import com.guet.main.entity.RoomReservation;
import com.guet.main.entity.vo.CheckVo;
import com.guet.main.service.CredentialsService;
import com.guet.main.service.RoomReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * credentials 前端控制器
 * </p>
 *
 * @author csp
 * @since 2022-09-04
 */
@RestController
@RequestMapping("/main/credentials")
@CrossOrigin
public class CredentialsController {
    @Autowired
    private CredentialsService credentialsService;

    @Autowired
    private RoomReservationService roomReservationService;

    @PostMapping("/check")
    public R check(@RequestBody CheckVo checkVo){
        RoomReservation roomReservation = roomReservationService.getById(checkVo.getId());
        roomReservation.setCredentials(checkVo.getIsPass());
        boolean b = roomReservationService.updateById(roomReservation);
        return b?R.ok():R.error();
    }

    @GetMapping("/getByReservationId/{reservationId}")
    public R getByReservationId(@PathVariable String reservationId){
        QueryWrapper<Credentials> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("reservation_id",reservationId);
        List<Credentials> list = credentialsService.list(queryWrapper);
        if (!list.isEmpty()){
            return R.ok().data("credentials",list.get(0));
        }else {
            return R.error();
        }
    }

    @PostMapping("/add")
    public R add(@RequestBody Credentials credentials){
        RoomReservation roomReservation = roomReservationService.getById(credentials.getReservationId());
        roomReservation.setCredentials("1");
        roomReservationService.updateById(roomReservation);
        credentialsService.save(credentials);
        return R.ok();
    }
}

